import { Init, Provide } from '@midwayjs/core';
import { Role } from '../entity/role.entity';
import { Repository } from 'typeorm';
import { InjectEntityModel } from '@midwayjs/typeorm';

@Provide()
export class RoleService {
    private roles: Role[] = [];

    @InjectEntityModel(Role)
    roleModel: Repository<Role>;

    @Init()
    async init() {
        this.roles = await this.roleModel.find();
    }

    getRoleByCode(code: string): Role | undefined {
        return this.roles.find(r => r.code === code);
    }

    getAllRoles(): Role[] {
        return this.roles;
    }
}
